
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-10 09:38:02
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-01 11:56:18
 */
import { GameFacade } from "../libs/core/GameFacade";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { SceneManager } from "../libs/manager/SceneManager";
import { hex_md5 } from "../libs/md5/Md5";
import HttpPromise from "../libs/network/HttpPromise";
import rewardItem from "../views/rewardItem";
import bagCtr from "./bags/bagCtr";
import barrackCtr from "./barracks/barrackCtr";
import emailCtr from "./email/emailCtr";
import gameCtr from "./gameCtr";
import signCtr from "./sign/signCtr";
import treasureCtr from "./treasure/treasureCtr";

export const HttpConfig = {

  DOMAIN_NAME: "//guard.game4.cn/",
  // DOMAIN_NAME: "http://192.168.31.233:4000/",

  SOCKET_NAME: "//socket.fkxzg.game4.cn",

  MODEL_NAME: "",

  HEADERS_TEXT: { "Content-Type": "application/x-www-form-urlencoded" },

  HEADERS_JSON: { "Content-Type": "application/json" }
}
export default class netCtr {
  private static _ins: netCtr = null;

  public static get ins(): netCtr {
    if (this._ins == null) {
      this._ins = new netCtr();
    }
    return this._ins;
  }

  /**
 * 
 * 群黑登录
 */
  public qhlogin() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "login/qhLogin",
      method: 'post',
      data: {
        username: this.getParam("username"),
        serverid: this.getParam("serverid"),
        isadult: this.getParam("isadult"),
        time: this.getParam("time"),
        flag: this.getParam("flag"),
      },
      timetout: 3000,
      responseType: null,
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)
      .then((res) => {
        console.log("then")
        console.log(res)
      })
      .catch((err) => {
        console.log("catch")
        // console.log(err)
      })
  }

  /**
  * 登录游戏
  */
  public login() {
    let nowTime = new Date().getTime();
    let key = "8c6c8612ea9f077b972e5aa595769448";
    // console.log(decodeURIComponent(this.getParam("nname")))
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/enterGame",
      method: 'post',
      data: {
        openId: this.getParam("username"),
        avatar: this.getParam("avatar") || '1',
        nickName: decodeURIComponent(this.getParam("nname")) || '1',
        serverId: 1,
        gameUrl: '1',
        ecid: '1',
        time: nowTime.toString(),
        sign: hex_md5(this.getParam("username") + 1 + nowTime + key),
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {
        // console.log(res.data.data.userRes);
        let userRes = res.data.data.userRes;
        gameCtr.ins.roleId = userRes.roleId;
        gameCtr.ins.avatar = userRes.avatar;
        gameCtr.ins.nickName = userRes.nickName;
        gameCtr.ins.ranksArr = userRes.ranksArr;
        gameCtr.ins.diamonds = userRes.diamonds;
        gameCtr.ins.Integral = userRes.integral;

        gameCtr.ins.guideId = userRes.guideId;
        gameCtr.ins.victoryNum = userRes.victoryNum;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        gameCtr.ins.initVictoryData(res.data.data.victoryRes);

        emailCtr.ins.initData(res.data.data.emailRes);

        signCtr.ins.initData(res.data.data.signRes);

        bagCtr.ins.initData(res.data.data.bagRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);
      })
      .catch((err) => {

      })
  }

  /**
  * 解锁英雄
  */
  public bagUpdate(id, type = 1) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/bagUpdate",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        type
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = obj.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;
        bagCtr.ins.initData(res.data.data.bagRes);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_BAGCOM_UDPATE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 解锁英雄
   */
  public unLock(id) {
    let barrackEny = barrackCtr.ins.getBarrackById(id);
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/unLock",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        needDia: barrackEny.lock,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = obj.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 上阵 休息 切换
   */
  public setState(id) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/setState",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        ranks: gameCtr.ins.curretRanks - 1,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = obj.data.userRes;

        gameCtr.ins.ranksArr = userRes.ranksArr;
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);
        SceneManager.getInstance().removeLayer("detailLayer");
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 获取宝箱
   * @param id 
   */
  public getTreasure(id, score) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/getTreasure",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        score
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        console.log(obj);
        let treasureRes = res.data.data.treasureRes;
        if (treasureRes.length > treasureCtr.ins.treasureEnyDic.length()) {
          let ele = treasureRes.pop();
          gameCtr.ins.curScene.emit("drawReward", { type: 3, id: ele.treasureId, num: 1 })
        }

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 获取宝箱奖励
   * @param id 
   */
  public getReward(id, uid) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/getReward",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        uid: uid,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        console.log(obj);
        let arr = obj.data.arr;
        arr.forEach((element, i) => {
          setTimeout(() => {
            let node = gameCtr.ins.poolObj['rewardItem'].get();
            cc.find("tipLayer").addChild(node);
            node.getComponent(rewardItem).drawUi(element);
          }, i * 1000);
        });

        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_CLOSE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);

        this.getData();
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 开启宝箱
   * @param id 
   */
  public openTreasure(id, uid) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/openTreasure",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id: id,
        uid
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        console.log(obj);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 加速宝箱
   * @param id 
   */
  public accelerateTreasure(id, uid, needD) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/accelerate",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id,
        uid,
        needD
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        console.log(obj);
        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 加速宝箱
   * @param id 
   */
  public integral(type) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/integral",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        type
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = res.data.data.userRes;
        gameCtr.ins.Integral = userRes.integral;

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
  * 加速宝箱
  * @param id 
  */
  public guide() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/guide",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }
        let userRes = res.data.data.userRes;
        gameCtr.ins.guideId = userRes.guideId;

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 获取累胜奖励
   */
  public getVictoryReward(id) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/victory",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        gameCtr.ins.initVictoryData(res.data.data.victoryRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_VICTORY_UPDATE);

        console.log(obj);
        let arr = obj.data.arr;
        arr.forEach((element, i) => {
          setTimeout(() => {
            let node = gameCtr.ins.poolObj['rewardItem'].get();
            cc.find("tipLayer").addChild(node);
            node.getComponent(rewardItem).drawUi(element);
          }, i * 1000);
        });

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 一键领取
   */
  public yjLq() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/yjLq",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);

        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_CLOSE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);

        SceneManager.getInstance().pushLayer("tipLayer", "领取成功!");

      })
      .catch((err) => {
        console.log(err)
      })
  }


  /**
   * 一键领取
   */
  public yjDel() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/yjDel",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);

        SceneManager.getInstance().pushLayer("tipLayer", "操作成功!");

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 领取邮件奖励
   */
  public getEmailList() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/getEmailList",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);

        // GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);
        SceneManager.getInstance().pushLayer("emailLayer");
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 领取邮件奖励
   */
  public lqEamil(id) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/lqEmail",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        uid: id + ""
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);
        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_CLOSE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);

        console.log(obj);
        let arr = obj.data.arr;
        arr.forEach((element, i) => {
          setTimeout(() => {
            let node = gameCtr.ins.poolObj['rewardItem'].get();
            cc.find("tipLayer").zIndex = 999;
            cc.find("tipLayer").addChild(node);
            node.getComponent(rewardItem).drawUi(element);
          }, i * 1000);
        });

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 删除奖励
   */
  public delEmail(id) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/delEmail",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        uid: id + ""
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 修改昵称
   */
  public updateNickName(nickName) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/updateNickName",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        nickName
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        gameCtr.ins.nickName = nickName;

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);

        SceneManager.getInstance().pushLayer("tipLayer", "修改成功!");

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
  * 兑换码
  */
  public exchange(id) {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/exchange",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
        id
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_CLOSE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);

        console.log(obj);
        let arr = obj.data.arr;
        arr.forEach((element, i) => {
          setTimeout(() => {
            let node = gameCtr.ins.poolObj['rewardItem'].get();
            cc.find("tipLayer").zIndex = 999;
            cc.find("tipLayer").addChild(node);
            node.getComponent(rewardItem).drawUi(element);
          }, i * 1000);
        });
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
 * 签到
 */
  public sign() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/sign",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;
        signCtr.ins.initData(res.data.data.signRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_SIGN_UDPATE);

        console.log(obj);
        let arr = obj.data.arr;
        arr.forEach((element, i) => {
          setTimeout(() => {
            let node = gameCtr.ins.poolObj['rewardItem'].get();
            cc.find("tipLayer").zIndex = 999;
            cc.find("tipLayer").addChild(node);
            node.getComponent(rewardItem).drawUi(element);
          }, i * 1000);
        });

        this.getData();
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 排名
   */
  public rankList() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/rankList",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          SceneManager.getInstance().pushLayer("tipLayer", obj.message);
          return;
        }

        console.log(obj.data);
        let pvpList = obj.data.pvpList;
        pvpList.forEach((ele, i) => {
          ele.rank = i + 1;
          if (ele.roleId == gameCtr.ins.roleId) {
            gameCtr.ins.myPvpRank = i + 1;
          }
        })
        gameCtr.ins.pvpList = pvpList;

        let mxList = obj.data.mxList;
        mxList.forEach((ele, i) => {
          ele.rank = i + 1;
          if (ele.roleId == gameCtr.ins.roleId) {
            gameCtr.ins.myMxRank = i + 1;
          }
        })
        gameCtr.ins.mxList = mxList;

        let wMxList = obj.data.wMxList;
        wMxList.forEach((ele, i) => {
          ele.rank = i + 1;
          if (ele.roleId == gameCtr.ins.roleId) {
            gameCtr.ins.myWMxRank = i + 1;
          }
        })
        gameCtr.ins.wMxList = wMxList;

        let wPvpList = obj.data.wPvpList;
        wPvpList.forEach((ele, i) => {
          ele.rank = i + 1;
          if (ele.roleId == gameCtr.ins.roleId) {
            gameCtr.ins.myWPvpRank = i + 1;
          }
        })
        gameCtr.ins.wPvpList = wPvpList;

        SceneManager.getInstance().pushLayer("rankLayer");
      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
 * 签到
 */
  public getData() {
    const data: any = {
      url: HttpConfig.DOMAIN_NAME + "guard/getData",
      method: 'post',
      data: {
        roleId: gameCtr.ins.roleId,
      },
      timetout: 30000,
      responseType: "text",
      headers: HttpConfig.HEADERS_TEXT,
    }

    return HttpPromise.ins.xhrRequest(data)

      .then((res: any) => {

        let obj = res.data;

        if (obj.code != 0) {
          return;
        }

        emailCtr.ins.initData(res.data.data.emailRes);

        let userRes = res.data.data.userRes;
        gameCtr.ins.diamonds = userRes.diamonds;

        let barrackRes = res.data.data.barrackRes;
        barrackCtr.ins.initData(barrackRes);

        let treasureRes = res.data.data.treasureRes;
        treasureCtr.ins.initData(treasureRes);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_CLOSE);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_USER_DATA);
        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);

        GameFacade.getInstance().sendNotification(PureMVCEvents.ON_EMAIL_UPDATE);

      })
      .catch((err) => {
        console.log(err)
      })
  }

  /**
   * 获取参数
   * @param str 
   * @param key 
   */
  public getParam(key: string): any {

    let str = this.getsearchUrl();

    let strArr: Array<any> = str.substring(1).split("&");

    for (let i: number = 0; i < strArr.length; i++) {

      let paramsArr: Array<any> = strArr[i].split("=");

      if (paramsArr[0] == key) {

        return paramsArr[1];
      }
    }
    return null;
  }

  /**
  * 获取地址栏参数
  */
  public getsearchUrl(): string {

    let searchUrl: string = "";

    let e = CC_DEBUG ? 1 : 2;
    // console.log("e:", e)
    if (e == 2) {
      searchUrl = window.location.search;
    } else if (e == 1) {
      let sanguoName = localStorage.getItem("sanguoName");
      sanguoName = "71515517148";
      if (!sanguoName) {

        sanguoName = "7151127" + Math.floor(Math.random() * 100);

        localStorage.setItem("sanguoName", sanguoName);
      }

      searchUrl = "?username=" + sanguoName + "&serverid=1&time=1571710275&closepay=2&showad=0&flag=8345addc4d2d8a16c62e24e81bc8a362&unid=&showbtn=0&qhchannel=qunhei_1571710275_7154417&qhchannelid=7154417&nname=%E3%80%82%E3%80%82%E3%80%82&uimg=https%3A%2F%2Fm.qunhei.com%2FStatic%2FDefault%2FHome%2Fimages%2Fbg_touxiang_wutouxiang.png&isadult=1";

    }

    return searchUrl;

  }
}